<?php
/**
 * User: Administrator
 * Date: 13-9-14
 * Time: 上午10:51
 * 菜单数据管理
 */

class MenuModel extends CommonModel
{
    /**
     * 读取树型数据
     * @return mixed
     */
    public function fetch_tree_list()
    {
        return $this
            ->field('menu_id as id, menu_name as text, parent_id as pid')
            ->order("parent_id ASC, order_by DESC")
            ->select();
    }

    /**
     * 读取子级菜单
     * @param int $parent
     * @return mixed
     */
    public function fetch_children($parent = 0)
    {
        $data = $this
            ->field('menu_id, menu_name, class_name, method_name, query_string')
            ->where("parent_id = $parent AND displayed = 1")
            ->order("order_by DESC")
            ->select();
        if($data){
            foreach($data as $key => $row){
                $row['query_string'] = (empty($row['query_string']) ? 'menuid=' : $row['query_string'] . '&menuid=') . $row['menu_id'];
                $row['href'] = format_url($row['class_name'], $row['method_name'], $row['query_string']);
                $data[$key] = $row;
            }
        }
        return $data;
    }
}